local U = require "packages/utility/utility"
local getAllCardNames = function(card_type, true_name)
    local all_names = {}
    local basics = {}
    local normalTricks = {}
    local delayedTricks = {}
    local equips = {}
    for _, card in ipairs(Fk.cards) do
        if card.package and not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and not card.is_derived then
            if card.type == Card.TypeBasic then
                table.insertIfNeed(basics, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick then
                table.insertIfNeed(normalTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type == Card.SubtypeDelayedTrick then
                table.insertIfNeed(delayedTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeEquip then
                table.insertIfNeed(equips, true_name and card.trueName or card.name)
            end
        end
    end
    if card_type:find("b") then
        table.insertTable(all_names, basics)
    end
    if card_type:find("t") then
        table.insertTable(all_names, normalTricks)
    end
    if card_type:find("d") then
        table.insertTable(all_names, delayedTricks)
    end
    if card_type:find("e") then
        table.insertTable(all_names, equips)
    end
    return all_names
end
local cailu = fk.CreateSkill {
    name = "ym1__cailu",
    dynamic_desc = function (self, player, lang)
        local str = "一名角色的回合结束时，你可选择一名本回合发动过不在其武将牌上技能的其他角色，令其交给你一张不在游戏牌堆内的牌。若做不到，将其的一个技能加入你的“引文”。"
        local mark = player:getTableMark("@[ym1__yinwen]")
        if #mark > 0 then
            for _, value in ipairs(mark) do
                str = str .. "<br><br><i><font color = '#a40000'><small>“"..Fk:translate(":"..value).."”——"..Fk:translate(value).."</font></i>"
            end
        end
        return str
    end,
}
Fk:addQmlMark{
    name = "ym1__yinwen",
    how_to_show = function(name, value)
        if type(value) == "string" then
            return Fk:translate(value)
        elseif type(value) == "table" then
            return tostring(#value)
        end
        return " "
    end,
    qml_path = "packages/ym_Al/qml/YinwenDetailBox"
}
Fk:loadTranslationTable{
    ["ym1__cailu"] = "采录",
    [":ym1__cailu"] = "一名角色的回合结束时，你可选择一名本回合发动过不在其武将牌上技能的其他角色，令其交给你一张不在游戏牌堆内的牌。若做不到，将其的一个技能加入你的“引文”。<br><br><i><font color = '#a40000'><small>“持恒技，摸牌阶段，你多摸一张牌。”——英姿</font></i>",
    ["#ym1__cailu-choose"] = "是否发动 采录，采录一名其他角色的技能？",
    ["#ym1__cailu-give"] = "采录：交给 %dest 一张不在游戏牌堆内的牌，否则其采录你的一个技能",
    ["#ym1__cailu-skill"] = "请你采录 %dest 的一个技能",
    ["@[ym1__yinwen]"] = "引文",

    ["$ym1__cailu1"] = "君子为学，必考信于竹帛，析理于毫芒。",
    ["$ym1__cailu2"] = "酌古之要，通今之宜，秉直笔以明道。",
}
cailu:addEffect(fk.TurnEnd,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(cailu.name) then
            for _, sp in ipairs(player.room:getOtherPlayers(player, false)) do
                for _, s in ipairs(sp:getAllSkills()) do
                    if sp:usedSkillTimes(s:getSkeleton().name, Player.HistoryTurn) ~= 0 and not table.contains(Fk.generals[sp.general]:getSkillNameList(true),s:getSkeleton().name) and
                    (sp.deputyGeneral == "" or not table.contains(Fk.generals[sp.deputyGeneral]:getSkillNameList(true),s:getSkeleton().name)) then
                        return true
                    end
                end
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = {}
        for _, sp in ipairs(player.room:getOtherPlayers(player, false)) do
            for _, s in ipairs(sp:getAllSkills()) do
                if sp:usedSkillTimes(s:getSkeleton().name, Player.HistoryTurn) ~= 0 and not table.contains(Fk.generals[sp.general]:getSkillNameList(true),s:getSkeleton().name) and
                (sp.deputyGeneral == "" or not table.contains(Fk.generals[sp.deputyGeneral]:getSkillNameList(true),s:getSkeleton().name)) then
                    local player_skills = {}
                    for _, skill in ipairs(sp.player_skills) do
                        if skill:isPlayerSkill(sp) and not skill:getSkeleton().name:startsWith("#") and not skill.attached_equip then
                            table.insert(player_skills, skill:getSkeleton().name)
                        end
                    end
                    if #player_skills > 0 then
                        table.insertIfNeed(targets,sp)
                    end
                end
            end
        end
        if #targets > 0 then
            local to = room:askToChoosePlayers(player,{
                targets = targets,
                max_num = 1,
                min_num = 1,
                prompt = "#ym1__cailu-choose",
                skill_name = cailu.name,
                cancelable = true,
            })
            if #to > 0 then
                event:setCostData(self, {tos = to})
                return true
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local cards = table.filter(player:getCardIds("he"), function (id)
            local name = Fk:getCardById(id).name
            return not table.contains(getAllCardNames("btde"),name)
        end)
        local card = room:askToCards(to,{
            max_num = 1,
            min_num = 1,
            include_equip = true,
            skill_name = cailu.name,
            cancelable = true,
            pattern = tostring(Exppattern{ id = cards }),
            prompt = "#ym1__cailu-give::"..player.id
        })
        if #card > 0 then
            room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonGive, cailu.name, nil, false, to)
        else
            local player_skills = {}
            for _, skill in ipairs(to.player_skills) do
                if skill:isPlayerSkill(to) and not skill:getSkeleton().name:startsWith("#") and not skill.attached_equip then
                    table.insert(player_skills, skill:getSkeleton().name)
                end
            end
            local choice = room:askToChoice(player,{
                choices = player_skills,
                skill_name = cailu.name,
                prompt = "#ym1__cailu-skill::"..to.id,
                detailed = true
            })
            room:handleAddLoseSkills(to, "-"..choice, nil, true, false)
            local mark = player:getTableMark("@[ym1__yinwen]")
            table.insert(mark,choice)
            room:setPlayerMark(player,"@[ym1__yinwen]",mark)
        end
    end,
})
cailu:addAcquireEffect(function (self, player, is_start)
    if type(player:getMark("@[ym1__yinwen]")) ~= "table" then
        player.room:setPlayerMark(player,"@[ym1__yinwen]",{"ym1__yingzi"})
    end
end)
return cailu